//
//  RedioDetailViewController.m
//  Leisure
//
//  Created by 大展 on 16/4/20.
//  Copyright © 2016年 张展. All rights reserved.
//

#import "RedioDetailViewController.h"

@interface RedioDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UILabel *dec;
@property (strong, nonatomic) IBOutlet UILabel *count;
@property (strong, nonatomic) IBOutlet UIImageView *imgView;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation RedioDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"RadioDetailCell" bundle:nil] forCellReuseIdentifier:@"radioDetail_cell"];
    [self fetchDataWithUrl:kRedioDetailUrl withParamater:@{@"radioid":self.model.radioid}];
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:self.model.coverimg]];
    self.count.text = [NSString stringWithFormat:@"%ld", (long)self.model.count];
    self.dec.text = self.model.desc;
    self.name.text = self.model.uname;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)fetchDataWithUrl:(NSString *)url withParamater:(NSDictionary *)parameter{


    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

            NSArray *array = dic[@"data"][@"list"];

            for (NSDictionary *object in array) {

                RedioDetailModel *model = [[RedioDetailModel alloc]init];
                [model setValuesForKeysWithDictionary:object];
                [self.dataSource addObject:model];
            }
            NSLog(@"1");

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.tableView reloadData];
            });
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"0");
        }];


    });
   }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RadioDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radioDetail_cell" forIndexPath:indexPath];

    [cell setCellWithModel:self.dataSource[indexPath.row]];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    RadioPlayViewController *radioPalyVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RadioPlayViewController"];;

    radioPalyVC.indexPath = indexPath.row;
    NSLog(@"%ld",(long)indexPath.row);
    radioPalyVC.dataSource = self.dataSource;

    [self.navigationController pushViewController:radioPalyVC animated:YES];
    
}



- (NSMutableArray *)dataSource {

    if (!_dataSource) {

        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
